//
//  LGNavgationBar.m
//  UniverseProject
//
//  Created by king on 2019/9/4.
//  Copyright © 2019年 Cloud2209. All rights reserved.
//

#import "LGNavgationBar.h"

@implementation LGNavgationBar

+ (instancetype)loadBarWithView:(UIView *)view{
    LGNavgationBar *bar = [[LGNavgationBar alloc] initWithFrame:CGRectMake(0, 0, K_SCREEN_WIDTH, k_SafeAreaTopHeight)];
    [view addSubview:bar];
    return bar;
}


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        UILabel *titleLab = [UILabel labelWithTitle:@"" color:LGCOLOR(@"000000") font:LGFONT(18)];
        titleLab.frame = CGRectMake(0, k_StatusBarHeight, K_SCREEN_WIDTH, 44);
        titleLab.textAlignment = 1;
        [self addSubview:titleLab];
        self.titleLab = titleLab;
    }
    return self;
}


- (void)setRightItems:(NSArray *)rightItems{
    _rightItems = rightItems;
    
    self.rightViewItems = [NSMutableArray array];
    CGFloat padding = 15;
    CGFloat itemW = 40;
    for (int i = 0; i < rightItems.count; i ++) { //正常情况下 navbar 只有一个文字按钮样式的
        NSString *itemTitle = rightItems[i];
        UIButton *item = [UIButton buttonWithType:UIButtonTypeCustom];
        item.tag = i;
        [item addTarget:self action:@selector(lgNavigationBaritemClick:) forControlEvents:UIControlEventTouchUpInside];
        if ([itemTitle hasSuffix:@".png"] || [itemTitle hasSuffix:@".jpg"]) {
            [item setImage:[self imageWithOriginalRenderingMode:itemTitle] forState:UIControlStateNormal];
            item.frame = CGRectMake(K_SCREEN_WIDTH - (i + 1) * (itemW+padding), k_StatusBarHeight, itemW, 44);
        }else {
            [item setTitle:itemTitle forState:UIControlStateNormal];
            [item setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            item.titleLabel.font = LGFONT(14);
            CGFloat width = [itemTitle sizeWithFont:LGFONT(14)].width;
//            CGSize *itemSize = [item.titleLabel sizeThatFits:cgsize]
            item.frame = CGRectMake(K_SCREEN_WIDTH - (i + 1) * (width+padding), k_StatusBarHeight, width, 44);
        }
        [self addSubview:item];
        [self.rightViewItems addObject:item];
    }
    
}


- (UIImage *)imageWithOriginalRenderingMode:(NSString *)imageName
{
    UIImage *image = [UIImage imageNamed:imageName];
    return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}




- (void)lgNavigationBaritemClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(lgNavBar:didClickIndex:)]) {
        [self.delegate lgNavBar:self didClickIndex:btn.tag];
    }
}


@end
